﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Target = KC.Business.Net;
using DAL = KC.DataAccess;

namespace KC.Tests.Business.Net
{
	namespace Tracking
	{
		[TestClass]
		public class Spiders : Target.Tracking.Spiders
		{
			[TestMethod]
			[TestCategory("Unit")]
			public void isSpiderTest()
			{
				foreach (string UserAgent2 in DAL.Net.Tracking.Spiders.GetSpiderUserAgents())
					Assert.IsTrue(Target.Tracking.Spiders.isSpider(UserAgent2, "0.0.0.0"));
			}
			[TestMethod]
			[TestCategory("Unit")]
			public void GetQueryMemberTest()
			{
				foreach (string engine in new String[]{
					"google.com", "live.com", "msn.com", "altavista.com",
					"search.com", "ask.com", "cuil.com", "bing.com"})
					Assert.IsTrue(GetQueryMember(engine) == "q");
				foreach (string engine in new string[]{ "lycos.com", "business.com", "aol.com", "netscape.com", "clusty.com"})
					Assert.IsTrue(GetQueryMember(engine) == "query");

				Assert.IsTrue(GetQueryMember("yahoo.com") == "p");
				Assert.IsTrue(GetQueryMember("baidu.com") == "wd");
			}
			[TestMethod]
			[TestCategory("Unit")]
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
			public void GetQueryMemberNullTest1()
			{
				GetQueryMember("wysiwyg");
			}
			[TestMethod]
			[TestCategory("Unit")]
			[ExpectedException(typeof(ArgumentNullException))]
			public void GetQueryMemberNullTest2()
			{
				GetQueryMember(null);
			}
			[TestMethod]
			[TestCategory("Unit")]
			[ExpectedException(typeof(ArgumentNullException))]
			public void isSpiderNullTest()
			{
				Target.Tracking.Spiders.isSpider("asdfasdfasdfasdfasdfasdfasdf", null);
			}

			public const string Url1 = @"http://www.google.com/#hl=en&source=hp&q=Holy+Smoke&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=ce970626fb1f491a";
			public const string Url2 = @"http://search.yahoo.com/search;_ylt=AobKMqIzwBD3ZHSq7vl1EtCbvZx4?p=holy+smoke&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701";
			public const string Url3 = @"http://search.lycos.com/?tab=web&query=holy+smoke";
			public const string UrlInvalid = @"http://www.kconnolly.net/";

			[TestMethod]
			[TestCategory("Unit")]
			public void TryParseSpiderReferralTest1()
			{
				string Engine, Query;
				bool result = Target.Tracking.Spiders.TryParseSpiderReferral(new Uri(Url1), out Engine, out Query);
				Assert.IsTrue(result);
				Assert.AreEqual(Engine, "google");
				Assert.AreEqual(Query, "Holy Smoke");
			}
			[TestMethod]
			[TestCategory("Unit")]
			public void TryParseSpiderReferralTest2()
			{
				string Engine, Query;
				bool result = Target.Tracking.Spiders.TryParseSpiderReferral(new Uri(UrlInvalid), out Engine, out Query);
				Assert.IsFalse(result);
				Assert.IsNull(Engine);
				Assert.IsNull(Query);
			}
			[TestMethod]
			[TestCategory("Unit")]
			public void TryParseSpiderReferralTest3()
			{
				string Engine, Query;
				bool result = Target.Tracking.Spiders.TryParseSpiderReferral(new Uri(Url2), out Engine, out Query);
				Assert.IsTrue(result);
				Assert.AreEqual(Engine, "yahoo");
				Assert.AreEqual(Query, "holy smoke");
			}
			[TestMethod]
			[TestCategory("Unit")]
			public void TryParseSpiderReferralTest4()
			{
				string Engine, Query;
				bool result = Target.Tracking.Spiders.TryParseSpiderReferral(new Uri(Url3), out Engine, out Query);
				Assert.IsTrue(result);
				Assert.AreEqual(Engine, "lycos");
				Assert.AreEqual(Query, "holy smoke");
			}
			[TestCategory("Unit")]
			public void TryParseSpiderReferralNullTest()
			{
				string Engine, Query;
				bool result = Target.Tracking.Spiders.TryParseSpiderReferral(null, out Engine, out Query);
				Assert.IsFalse(result);
				Assert.IsNull(Engine);
				Assert.IsNull(Query);
			}
		}
	}

	[TestClass]
	public class Web : Target.Web
	{
		private const string
			ValidImage = @"http://www.google.com/intl/en_ALL/images/srpr/logo1w.png",
			InvalidImage = @"http://www.example.com/asdfasdfasdfasdf.jpg",
			ValidUrl = @"http://www.kconnolly.net/",
			InvalidUrl = @"http://www.example.com/asdfasdfasdfasdfasdf/asdfasdf.aspx";
		private readonly string ValidHtmlMarkup =
			"<html><head><title>test</title></head><body> \r\n" +
			"	Good Hyperlink	[<a href=\"" + ValidUrl + "\">Good Link</a>] \r\n" +
			"	Bad Hyperlink	[<a href=\"" + InvalidUrl + "\">Bad Link</a>] \r\n" +
			"	Bad Image		[<img src=\"" + InvalidImage + "\" />] \r\n" +
			"	Good Image		[<img src=\"" + ValidImage + "\" />] \r\n" +
			"</body></html>";

		[TestMethod]
		[TestCategory("Unit")]
		public void BuildListOfImgTagsTestAll()
		{
			List<string> result = BuildListOfImgTags(ValidHtmlMarkup, HyperlinkOption.All);
			Assert.AreEqual(result.Count, 2);
			Assert.IsTrue(result.Contains(ValidImage));
			Assert.IsTrue(result.Contains(InvalidImage));
		}
		[TestMethod]
		[TestCategory("Internet")]
		public void BuildListOfImgTagsTestWorking()
		{
			List<string> result = BuildListOfImgTags(ValidHtmlMarkup, HyperlinkOption.Working);
			Assert.AreEqual(result.Count, 1);
			Assert.AreEqual(result[0], ValidImage);
		}
		[TestMethod]
		[TestCategory("Internet")]
		public void BuildListOfImgTagsTestBroken()
		{
			List<string> result = BuildListOfImgTags(ValidHtmlMarkup, HyperlinkOption.Broken);
			Assert.AreEqual(result.Count, 1);
			Assert.AreEqual(result[0], InvalidImage);
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void BuildListOfImgTagsNullTest1()
		{ BuildListOfImgTags(null, HyperlinkOption.Broken); }

		[TestMethod]
		[TestCategory("Unit")]
		public void GetLinksFromHtmlTest1()
		{
			List<string> result = GetLinksFromHtml(ValidHtmlMarkup);
			Assert.AreEqual(result.Count, 2);
			Assert.IsTrue(result.Contains(ValidUrl));
			Assert.IsTrue(result.Contains(InvalidUrl));
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void GetListFromHtmlNullTest()
		{ GetLinksFromHtml(null); }

		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void GetLinksNullTest1()
		{ GetLinks(null, HyperlinkOption.All); }
		[TestMethod]
		[TestCategory("Unit")]
		public void GetLinksNullTest2()
		{ 
			List<string> result = GetLinks(new List<string>(), HyperlinkOption.All);
			Assert.IsNotNull(result);
			Assert.AreEqual(result.Count, 0);
		}

	}
}
